/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.sys.modular.coursehomework.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.repository.query.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.core.annotion.BusinessLog;
import vip.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.core.pojo.response.SuccessResponseData;
import vip.xiaonuo.sys.modular.coursehomework.param.HomeworkParam;
import vip.xiaonuo.sys.modular.coursehomework.param.HomeworkParamForQuery;
import vip.xiaonuo.sys.modular.coursehomework.param.HomeworkScoreSummaryParam;
import vip.xiaonuo.sys.modular.coursehomework.service.HomeworkService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 作业表控制器
 *
 * @author kongxm
 * @date 2022-07-16 14:40:04
 */
@RestController
public class HomeworkController {

    @Resource
    private HomeworkService homeworkService;

    /**
     * 查询作业表
     *
     * @author kongxm
     * @date 2022-07-16 14:40:04
     */

    @GetMapping("/homework/pageForTeacher")
    @BusinessLog(title = "作业表_查询_作业管理", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData pageForTeacher(HomeworkParamForQuery homeworkParamForQuery) {
        return new SuccessResponseData(homeworkService.pageForTeacher(homeworkParamForQuery));
    }


    /**
     * 查询作业表
     *
     * @author kongxm
     * @date 2022-07-16 14:40:04
     */

    @GetMapping("/homework/pageForStudent")
    @BusinessLog(title = "作业表_查询_作业管理", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData pageForStudent(HomeworkParamForQuery homeworkParamForQuery) {
        return new SuccessResponseData(homeworkService.pageForStudent(homeworkParamForQuery));
    }

    /**
     * 确认上传作业
     *
     * @author kongxm
     * @date 2022-07-16 14:40:04
     */

    @PutMapping("/homework/stuConfirm")
    @BusinessLog(title = "作业表_学生_确认上传", opType = LogAnnotionOpTypeEnum.UPDATE)
    public ResponseData stuConfirm(Long id) {
        homeworkService.stuConfirm(id);
        return new SuccessResponseData();
    }

    /**
     * 添加作业表
     *
     * @author kongxm
     * @date 2022-07-16 14:40:04
     */

    @PostMapping("/homework/add")
    @BusinessLog(title = "作业表_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(HomeworkParam.add.class) HomeworkParam homeworkParam) {
        homeworkService.add(homeworkParam);
        return new SuccessResponseData();
    }

    /**
     * 删除作业表，可批量删除
     *
     * @author kongxm
     * @date 2022-07-16 14:40:04
     */

    @PostMapping("/homework/delete")
    @BusinessLog(title = "作业表_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(HomeworkParam.delete.class) List<HomeworkParam> homeworkParamList) {
        homeworkService.delete(homeworkParamList);
        return new SuccessResponseData();
    }

    /**
     * 编辑作业表
     *
     * @author kongxm
     * @date 2022-07-16 14:40:04
     */

    @PostMapping("/homework/edit")
    @BusinessLog(title = "作业表_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(HomeworkParam.edit.class) HomeworkParam homeworkParam) {
        homeworkService.edit(homeworkParam);
        return new SuccessResponseData();
    }

    /**
     * 查看作业表
     *
     * @author kongxm
     * @date 2022-07-16 14:40:04
     */

    @GetMapping("/homework/detail")
    @BusinessLog(title = "作业表_查看", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(@Validated(HomeworkParam.detail.class) HomeworkParam homeworkParam) {
        return new SuccessResponseData(homeworkService.detail(homeworkParam));
    }

    /**
     * 作业表列表
     *
     * @author kongxm
     * @date 2022-07-16 14:40:04
     */

    @GetMapping("/homework/list")
    @BusinessLog(title = "作业表_列表", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(HomeworkParam homeworkParam) {
        return new SuccessResponseData(homeworkService.list(homeworkParam));
    }

    /**
     * 导出
     *
     * @author kongxm
     * @date 2022-07-16 14:40:04
     */

    @GetMapping("/homework/exportForTeacher")
    @BusinessLog(title = "作业表_导出_for_teacher", opType = LogAnnotionOpTypeEnum.EXPORT)
    public void homeworkExportForTeacher(HomeworkParamForQuery homeworkParamForQuery) {
        homeworkService.homeworkExportForTeacher(homeworkParamForQuery);
    }


    /**
     * 上传作业
     *
     * @author yubaoshan
     * @date 2020/6/7 22:15
     */
    @PostMapping("/homework/uploadHomework")
    @BusinessLog(title = "文件信息表_上传作业", opType = LogAnnotionOpTypeEnum.OTHER)
    public ResponseData uploadHomework(@RequestPart("courseSelectionId") String courseSelectionId, @RequestPart("file") MultipartFile file) {
        Long newId = StringUtils.isNotEmpty(courseSelectionId) ? Long.valueOf(courseSelectionId) : null;
        Long fileId = homeworkService.uploadHomework(newId, file);
        return new SuccessResponseData(fileId);
    }

    /**
     * 上传批改后的作业
     *
     * @author yubaoshan
     * @date 2020/6/7 22:15
     */
    @PostMapping("/homework/uploadCorrectedHomework")
    @BusinessLog(title = "文件信息表_上传批改后的作业", opType = LogAnnotionOpTypeEnum.OTHER)
    public ResponseData uploadCorrectedHomework(@RequestPart("id") String id, @RequestPart("file") MultipartFile file) {
        Long newId = StringUtils.isNotEmpty(id) ? Long.valueOf(id) : null;
        Long fileId = homeworkService.uploadCorrectedHomework(newId, file);
        return new SuccessResponseData(fileId);
    }

    /**
     * 分数统计
     *
     * @author kongxm
     * @date 2022-07-16 14:40:04
     */

    @GetMapping("/homework/scoreStatistics")
    @BusinessLog(title = "分数统计", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData queryScoreStatistics(HomeworkScoreSummaryParam homeworkScoreSummaryParam) {
        return new SuccessResponseData(homeworkService.queryScoreSummary(homeworkScoreSummaryParam));
    }

    /**
     * 导出系统用户
     *
     * @author kongxm
     * @date 2022-07-16 17:03:28
     */

    @GetMapping("/homework/scoreStatistics/export")
    @BusinessLog(title = "分数统计_导出", opType = LogAnnotionOpTypeEnum.EXPORT)
    public void exportScoreStatistics(HomeworkScoreSummaryParam homeworkScoreSummaryParam) {
        homeworkService.exportScoreStatistics(homeworkScoreSummaryParam);
    }

    /**
     * 批量下载学生作业
     *
     * @author kongxm
     * @date 2022-07-16 14:40:04
     */

    @PostMapping("/homework/batchDownloadStudentHomework")
    @BusinessLog(title = "作业表_批量导出学生作业", opType = LogAnnotionOpTypeEnum.EXPORT)
    public void batchDownloadStudentHomework(@RequestBody List<HomeworkParam> homeworkParams, HttpServletResponse response) {
        homeworkService.batchDownloadHomework(homeworkParams, 0, response);
    }

    /**
     * 批量下载批改后作业
     *
     * @author kongxm
     * @date 2022-07-16 14:40:04
     */

    @PostMapping("/homework/batchDownloadCorrectedHomework")
    @BusinessLog(title = "作业表_批量导出批改后作业", opType = LogAnnotionOpTypeEnum.EXPORT)
    public void batchDownloadCorrectedHomework(@RequestBody List<HomeworkParam> homeworkParams, HttpServletResponse response) {
        homeworkService.batchDownloadHomework(homeworkParams, 1, response);
    }

    /**
     * 批量上传批改后作业
     *
     * @author kongxm
     * @date 2022-07-16 14:40:04
     */

    @PostMapping("/homework/batchUploadCorrectedHomework")
    @BusinessLog(title = "作业表_批量上传批改后作业", opType = LogAnnotionOpTypeEnum.EXPORT)
    public ResponseData batchUploadCorrectedHomework(@RequestPart("file") MultipartFile file) throws Exception {
        homeworkService.batchUploadCorrectedHomework(file);
        return new SuccessResponseData();
    }

    /**
     * 查询开课轮次
     *
     * @author kongxm
     * @date 2022-07-16 14:40:04
     */

    @GetMapping("/homework/listFrequencyForTeacher")
    @BusinessLog(title = "作业表_查询开课轮次", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData listFrequencyForTeacher(Long courseId) {
        return new SuccessResponseData(homeworkService.listFrequencyForTeacher(courseId));
    }
}
